<template>
  <div>
    <Breadcrumb first="商品管理" second="分类参数"></Breadcrumb>
    <el-card class="box-card">
      <!-- 注意提示 -->
      <el-alert
        title="注意：只允许为第三级分类设置相关参数！"
        type="warning"
        effect="dark"
        show-icon
        :closable="false"
      ></el-alert>
      <br />
      <!-- 级联选择器 -->
      <div class="block">
        <span class="demonstration">选择商品分类：</span>
        <el-cascader
          v-model="value"
          :options="options"
          :props="{ expandTrigger: 'hover', value:'cat_id',label:'cat_name' }"
          @change="handleChange"
        ></el-cascader>
        =={{value}}==={{selectType}}
      </div>
      <br />

      <!-- 弹出层 -->
      <el-dialog
      title="添加参数"
        :visible.sync="dialogVisible"
        width="60%"
        :before-close="handleClose1"
        :destroy-on-close="true"
      >
        <!-- 添加动态参数 -->
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item :label="tapValue === 'many' ? '动态参数':'静态参数'" prop="attr_name">
            <el-input v-model="ruleForm.attr_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 卡片区域 -->
      <el-tabs type="border-card" v-model="tapValue" @tab-click="handleClick">
        <!-- 动态参数 表格- -->
        <el-tab-pane label="动态参数" name="many">
          <el-button type="primary" :disabled="selectType" @click="dialogVisible = true">添加参数</el-button>
          <br />
          <br />
          <el-table :data="tableData" :border="true" style="width: 100%">
            <el-table-column type="expand">
              <template slot-scope="props">
                <!-- slot-scope="props" -->
                <!-- tap标签 -->
                <el-form label-position="left" inline class="demo-table-expand">
                  <el-tag
                    :key="index"
                    v-for="tag,index in props.row.attr_vals.split (',').filter(obj=>obj!='')"
                    closable
                    :disable-transitions="false"
                    @close="handleClose(tag)"
                  >{{tag}}</el-tag>
                  <el-input
                    class="input-new-tag"
                    v-if="props.row.inputVisible"
                    v-model="props.row.inputValue"
                    ref="abc"
                    size="small"
                    @keyup.enter.native="handleInputConfirm($event,props.row)"
                    @blur="handleInputConfirm($event,props.row)"
                  ></el-input>
                  <el-button
                    v-else
                    class="button-new-tag"
                    size="small"
                    @click="showInput(props.row)"
                  >+ New Tag</el-button>
                  <!-- tap标签 -->
                </el-form>
              </template>
            </el-table-column>
            <el-table-column label="#" type="index"></el-table-column>
            <el-table-column label="动态属性" prop="attr_name"></el-table-column>
            <el-table-column label="操作" prop="desc">
              <template>
                <el-button type="primary" icon="el-icon-edit" size="mini">提交</el-button>
                <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <!-- 静态表格 -->
        <el-tab-pane label="配置管理" name="only">
          <el-button type="primary" :disabled="selectType" @click="dialogVisible = true">添加参数</el-button>
          <br>
          <br>
          <el-table :data="tableData" :border="true" style="width: 100%">
            <el-table-column type="expand">
              <template slot-scope="props">
                <!-- slot-scope="props" -->
                <!-- tap标签 -->
                <div class="box">
                  <el-tag
                    :key="index"
                    v-for="tag,index in props.row.attr_vals.split (',').filter(obj=>obj!='')"
                    closable
                    :disable-transitions="false"
                    @close="handleClose(tag)"
                  >{{tag}}</el-tag>
                  <el-input
                    class="input-new-tag"
                    v-if="props.row.inputVisible"
                    v-model="props.row.inputValue"
                    ref="abc"
                    size="small"
                    @keyup.enter.native="handleInputConfirm($event,props.row)"
                    @blur="handleInputConfirm($event,props.row)"
                  ></el-input>
                  <el-button
                    v-else
                    class="button-new-tag"
                    size="small"
                    @click="showInput(props.row)"
                  >+ New Tag</el-button>
                  <!-- tap标签 -->
                </div>
              </template>
            </el-table-column>
            <el-table-column label="#" type="index"></el-table-column>
            <el-table-column label="静态属性" prop="attr_name"></el-table-column>
            <el-table-column label="操作" prop="desc">
              <template>
                <el-button type="primary" icon="el-icon-edit" size="mini">提交</el-button>
                <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { categoriesApi, addAttributesApi, redactAttributesApi, addArgumentsApi } from '@/api'
export default {
  created () {
    categoriesApi().then((res) => {
      this.options = res.data.data
    })
  },
  data () {
    return {
      activeName: 'first',
      value: [],
      options: [],
      tableData: [],
      selectType: true,
      dynamicTags: [],
      // inputVisible: false,
      // inputValue: '',
      tapValue: 'many',
      dialogVisible: false,
      ruleForm: {
        attr_name: ''
      },
      rules: {
        attr_name: [
          { required: true, message: '请输入动态参数名称', trigger: 'blur' },
          { min: 5, max: 15, message: '长度在5到15个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    addAttributesFn () {
      addAttributesApi(this.value[this.value.length - 1], this.tapValue).then(
        (res) => {
          console.log(res)
          if (res.data.data !== null) {
            console.log(1)
            res.data.data.forEach((item) => {
              item.inputVisible = false
              item.inputValue = ''
            })
          }
          this.tableData = res.data.data
        }
      )
    },
    handleChange (value) {
      if (value.length === 3) {
        this.selectType = false
        this.addAttributesFn()
      } else {
        this.value = []
        this.$message({
          message: '只允许为三级分类设置参数！',
          type: 'warning'
        })
      }
    },

    handleClick (tab, event) {
      this.addAttributesFn()
    },
    // tap
    handleClose (tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1)
    },

    showInput (row) {
      row.inputVisible = true
      this.$nextTick((_) => {
        this.$refs.abc.$refs.input.focus()
      })
    },
    async handleInputConfirm (e, arr) {
      if (!arr.inputValue) {
        //  表单中没有值 不需要发送请求
        return false
      }
      var attrVal = arr.attr_vals ? arr.attr_vals.split(',') : []
      attrVal = [...attrVal, arr.inputValue].join(',')
      console.log(attrVal)
      /*  const inputValue = this.inputValue
      if (inputValue) {
        this.dynamicTags.push(inputValue)
      } */
      // console.log(this.dynamicTags)
      const res = await redactAttributesApi({
        id: this.value[this.value.length - 1],
        attr_name: arr.attr_name,
        attrId: arr.attr_id,
        attr_sel: this.tapValue,
        attr_vals: attrVal
      })
      console.log(res)
      arr.attr_vals = attrVal
      arr.inputVisible = false
      arr.inputValue = ''
    },
    handleClose1 (done) {
      this.ruleForm.attr_name = ''
      done()
    },
    submitForm (ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          this.dialogVisible = false
          addArgumentsApi({
            id: this.value[this.value.length - 1],
            attr_name: this.ruleForm.attr_name,
            attr_sel: this.tapValue
          }).then(res => {
            if (res.data.meta.status === 201) {
              this.$message({
                message: res.data.meta.msg,
                type: 'success'
              })
            } else {
              this.$message.error(res.data.meta.msg)
            }
            this.addAttributesFn()
          })
        } else {
          this.dialogVisible = true
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang='less' scoped>
.demo-table-expand {
  padding-left: 51px !important;
  .el-form-item {
    margin-bottom: 0;
  }
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
